import React, { act, Component } from 'react'
import store from '../../redux/store'
import {createDecrenmentAction,createIncrementAction} from '../../redux/count_action'
export default class Count extends Component {

    state={}
    // componentDidMount(){
    //     // 组件挂载的钩子函数 
    //     // 检测redux 中状态的变化 , 只要变化 就调用render
    //     store.subscribe(()=>{
    //         // redux 中 只有有状态的变化 都会进行回调
    //         // console.log("ssss")
    //         this.setState({})
    //     });
    // }

    increment = () => {
        // 获取当前的输入
        const { value } = this.selectNumber
        // console.log(value)
        // 通知 reducer 加值 
        store.dispatch(createIncrementAction(value*1))
        

        // const { count } = store.getState()
        // // value*1 是转换成 int 值 
        // this.setState({ count: count + value * 1 })
    }
    decrement = () => {
        // 当前求和的值 
        // const { count } = this.state
           const { value } = this.selectNumber
        // 当前选中的值 
        store.dispatch(createDecrenmentAction(value*1))
     
        // // 更新state
        // this.setState({ count: count -value*1})
    }
    // 奇数加 
    incrementIfOdd = () => {
        const  count =store.getState()
        const { value } = this.selectNumber
        if(count%2===1){
            store.dispatch(createIncrementAction(value*1))
        }
    }
    // 异步加 
    incrementAsync = () => {
        const { value } = this.selectNumber
        setTimeout(()=>{
            store.dispatch(createIncrementAction(value*1))
        },300)
        
    }

    render() {
        return (
            <div>
                <h1>当前的求和为:{store.getState()}</h1>
                <select ref={c => this.selectNumber = c} >
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                </select>
                &nbsp;  &nbsp;
                <button onClick={this.increment}>+</button>
                &nbsp; &nbsp;
                <button onClick={this.decrement}>-</button>
                &nbsp;  &nbsp;
                <button onClick={this.incrementIfOdd}>当前求和为奇数在加</button>
                &nbsp;  &nbsp;
                <button onClick={this.incrementAsync}>异步加</button>
            </div>
        )
    }
}
